<?php
/**
 * 认证模块后台 控制器
 *
 * @author    wangmx <wangmx@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Http\Controllers\Backstage;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Response;
use App\Services\UserService\authenticationBackstage;
use Maatwebsite\Excel\Facades\Excel;

class AuthenticationController extends BaseController
{

    protected $authen;

    public function __construct(authenticationBackstage $authen)
    {
        $this->authen = $authen;
    }

    /**
     * 设计师认证列表
     */
    public function AuthenticationToDesigner(Request $request)
    {
        $lists = $this->authen->AuthenticationToDesigner($request);
        return view('backstage.authentication.designerList')->with('lists', $lists);
    }

    /**
     * 品牌认证列表
     */
    public function AuthenticationToBrand(Request $request)
    {
        $lists = $this->authen->AuthenticationToBrand($request);
        return view('backstage.authentication.brandList')->with('lists', $lists);
    }


    /**
     * 设计师详情
     */
    public function designerDetail(Request $request)
    {
        $user_id = $request->get('user_id');
        $data = $this->authen->detail(1, $user_id);
        if ($data == false) {
            return redirect('/backstage/authentication/designer-list')->with('error_message', '查无信息');
        }
        return view('backstage.authentication.designerDetail')->with('data', $data);
    }

    /**
     * 品牌商详情
     */
    public function brandDetail(Request $request)
    {
        $user_id = $request->get('user_id');
        $data = $this->authen->detail(2, $user_id);
        if ($data == false) {
            return redirect('/backstage/authentication/brand-list')->with('error_message', '查无信息');
        }
        return view('backstage.authentication.brandDetail')->with('data', $data);
    }

    /**
     * 审核通过
     */
    public function passAuthentication(Request $request)
    {
        $result = $this->authen->passAuthentication($request);
        return Response::json($result);
    }


    /**
     * 审核不通过
     */
    public function RefusedAuthentication(Request $request)
    {
        $result = $this->authen->RefusedAuthentication($request);
        return Response::json($result);
    }

    /**
     * 推荐设计师
     */
    public function designerAuthentication(Request $request)
    {
        $authentication_type = 1;
        $user_id = $request->get('user_id');
        $result = $this->authen->commend($authentication_type, $user_id);
        if ($result['state'] == 'M00000') {
            return redirect()->back()->with('success_message', '设置推荐设计师成功');
        }
    }

    /**
     * 推荐品牌
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function brandAuthentication(Request $request)
    {
        $authentication_type = 2;
        $user_id = $request->get('user_id');
        $result = $this->authen->commend($authentication_type, $user_id);
        if ($result['state'] == 'M00000') {
            return redirect()->back()->with('success_message', '设置推荐品牌成功');
        }
    }

    /**
     * 取消设计师推荐
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function cancelDesignerAuthentication(Request $request)
    {
        $authentication_type = 1;
        $user_id = $request->get('user_id');
        $result = $this->authen->cancelCommend($authentication_type, $user_id);
        if ($result['state'] == 'M00000') {
            return redirect()->back()->with('success_message', '取消设计师推荐成功');
        }
    }

    /**
     * 取消品牌推荐
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function cancelBrandAuthentication(Request $request)
    {
        $authentication_type = 2;
        $user_id = $request->get('user_id');
        $result = $this->authen->cancelCommend($authentication_type, $user_id);
        if ($result['state'] == 'M00000') {
            return redirect()->back()->with('success_message', '取消品牌推荐成功');
        }
    }

    /**
     * 设置审核时间页面
     */
    public function config()
    {
        $timeout = DB::table('authentication_time_outs')->where('id', 1)->value('time_out');
        return view('backstage.authentication.config')->with('timeout', $timeout);
    }

    /**
     * 设置审核时间
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postconfig(Request $request)
    {
        $timeout = $request->get('timeout');
        $result = $this->authen->updateTimeout($timeout);
        if ($result['state'] == 'M00000') {
            return redirect()->back()->with('success_message', '修改成功');
        }
    }


    /**
     * 修改设计师认证信息
     */
    public function editDesigner(Request $request)
    {
        $this->authen->editDesigner($request);
        return redirect()->back()->with('success_message', '更新成功!');
    }

    /**
     * 修改品牌认证信息
     */
    public function editBrand(Request $request)
    {
        $this->authen->editBrand($request);
        return redirect()->back()->with('success_message', '更新成功!');
    }

    /**
     * @param Request $request
     * @return mixed
     * 更新品牌首页LOGO
     */
    public function updateLogo(Request $request)
    {
        $user_id = $request->get('user_id');
        $logo = DB::table('brand_authentications')->where('user_id', $user_id)->value('logo');
        $company_name = DB::table('brand_authentications')->where('user_id', $user_id)->value('company_name');
        $avatar = DB::table('users')->where('user_id', $user_id)->value('avatar');
        $avatarImg_substr = substr($avatar, 0, 4);
        if ($avatarImg_substr != 'http') {
            $avatar = asset('/images/' . $avatar);
        }
        return view('backstage.authentication.updateBrandLogo')->with(['logo' => $logo, 'company_name' => $company_name])->with('avatar', $avatar);
    }

    public function postUpdateLogo(Request $request)
    {
        $result = $this->authen->updateLogo($request);
        return redirect()->back()->with('success_message', '修改成功');
    }

    /**
     * 品牌名限制列表
     */
    public function limitNameList(){
        $lists = $this->authen->limitNameList();
        return view('backstage.authentication.limitList')->with('lists', $lists);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     * 删除
     */
    public function deleteLimit(Request $request)
    {
        $result = $this->authen->deleteLimit($request);
        return Response::json($result);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     * 添加
     */
    public function addLimit(Request $request)
    {
        $result = $this->authen->addLimit($request);
        return Response::json($result);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     * 编辑
     */
    public function editLimit(Request $request)
    {
        $result = $this->authen->editLimit($request);
        return Response::json($result);
    }

    /**
     * @param Request $request
     * @return mixed
     * 后台审核身份证
     */
    public function VerifiedCard(Request $request){
        $result = $this->authen->VerifiedCard($request);
        return Response::json($result);
    }


    public function postDownloadExcel()
    {
        $excel = DB::table('company_name_limits')->where('state',1)->orderBy('created_at', 'desc')->get()->toArray();
        $rows = array();
        for ($a = 0; $a < count($excel); $a++) {
            $keyword = $excel[$a]->keyword;
            $rows[] = array(
                '限制词' => $keyword,
            );
        }
        //判断文件夹是否存在
        if (!is_dir(public_path('images' . DIRECTORY_SEPARATOR . 'excel')))
            mkdir(public_path('images' . DIRECTORY_SEPARATOR . 'excel'));
        if (!is_dir(public_path('images' . DIRECTORY_SEPARATOR . 'excel' . DIRECTORY_SEPARATOR . 'user')))
            mkdir(public_path('images' . DIRECTORY_SEPARATOR . 'excel' . DIRECTORY_SEPARATOR . 'user'));

        $time = 'excel_' . time();
        Excel::create($time, function ($excel) use ($rows) {
            $excel->sheet('user', function ($sheet) use ($rows) {
                $sheet->fromArray($rows);
            });
        })->store('xls', public_path('images' . DIRECTORY_SEPARATOR . 'excel'));

        $obj = array('url' => asset('images/excel/' . $time . '.xls'));

        $result = array('message' => '成功', 'result' => $obj, 'state' => 'M00000');
        return $result;
    }

    public function postLoadExcel(Request $request)
    {
        $result = $this->authen->loadExcel($request);
        if ($result['state'] == 'M00000') {
            return redirect()->back()->with('success_message', '导入成功');
        } else {
            return redirect()->back()->with('error_message', '系统出错');
        }
    }
}
